/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.npi.todo.npitodo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.npi.todo.npitodo.entity.CommonResult;
import com.neusoft.npi.todo.npitodo.entity.ResultCode;
import com.neusoft.npi.todo.npitodo.entity.Todo;
import com.neusoft.npi.todo.npitodo.mapper.TodoMapper;
import com.neusoft.npi.todo.npitodo.service.TodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class TodoServiceImpl implements TodoService {
    @Autowired private TodoMapper todoMapper;

    @Override
    public CommonResult addTodo(Todo todo) {
        // 如果Entity对象在插入之前主键属性被赋值，那么自动生成主键不生效
        todo.setTodoid(null);
        int i = todoMapper.insert(todo);
        if (i > 0) return CommonResult.success(todo.getTodoid());
        return CommonResult.fail(ResultCode.INSERT_FAIL);
    }

    @Override
    public CommonResult deleteTodo(Integer tododi) {
        Todo todo = todoMapper.selectById(tododi);
        todo.setStatus("已删除");
        int i = todoMapper.updateById(todo);
        if (i > 0) return CommonResult.success(i);
        else return CommonResult.fail(ResultCode.DELETE_FAIL);
    }

    @Override
    public CommonResult findByid(Integer tododi) {
        Todo todo = todoMapper.selectById(tododi);
        if (Objects.isNull(todo)) return CommonResult.fail(ResultCode.NOT_FOUND);
        else return CommonResult.success(todo);
    }

    @Override
    public CommonResult findAll() {

        //        第一种：filter
        //        List<Todo> todos = todoMapper.selectList(null);
        //        List<Todo> todos1 = todos.stream().filter(todo ->
        // !todo.getStatus().equals("已删除")).toList();
        //        第二种
        //        QueryWrapper<Todo> queryWrapper = new QueryWrapper<>();
        //        queryWrapper.ne("status", "已删除");
        //        List<Todo> todos = todoMapper.selectList(queryWrapper);
        //  SELECT todoid,title,info,todotime,fulltime,endtime,benintime,status FROM todo WHERE
        // (status <> ?)
        List<Todo> todos = todoMapper.findAll();
        if (Objects.isNull(todos) || todos.size() == 0)
            return CommonResult.fail(ResultCode.NOT_FOUND);
        else return CommonResult.success(todos);
    }

    @Override
    public CommonResult startTodo(Integer todoid) {
        Todo todo = todoMapper.selectById(todoid);
        todo.setBenintime(new Date());
        todo.setStatus("进行中");
        int i = todoMapper.updateById(todo);
        if (i > 0) return CommonResult.success(i);
        else return CommonResult.fail(ResultCode.UPDATE_FAIL);
    }

    @Override
    public CommonResult finishTodo(Integer todoid) {
        Todo todo = todoMapper.selectById(todoid);
        if (Objects.isNull(todo.getBenintime()) || !todo.getStatus().equals("进行中"))
            return CommonResult.fail(ResultCode.UPDATE_FAIL);
        todo.setEndtime(new Date());
        todo.setStatus("已完成");
        int i = todoMapper.updateById(todo);
        if (i > 0) return CommonResult.success(i);
        else return CommonResult.fail(ResultCode.UPDATE_FAIL);
    }
}
